var e = require;
var t = module;
var o = exports;
var n,
    i = o,
    a =
        ((n = function (e, t) {
            return (n =
                Object.setPrototypeOf ||
                ({__proto__: []} instanceof Array &&
                    function (e, t) {
                        e.__proto__ = t;
                    }) ||
                function (e, t) {
                    for (var o in t) t.hasOwnProperty(o) && (e[o] = t[o]);
                })(e, t);
        }),
        function (e, t) {
            function o() {
                this.constructor = e;
            }
            n(e, t), (e.prototype = null === t ? Object.create(t) : ((o.prototype = t.prototype), new o()));
        }),
    r = function (e, t, o, n) {
        var i,
            a = arguments.length,
            r = a < 3 ? t : null === n ? (n = Object.getOwnPropertyDescriptor(t, o)) : n;
        if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(e, t, o, n);
        else
            for (var s = e.length - 1; s >= 0; s--)
                (i = e[s]) && (r = (a < 3 ? i(r) : a > 3 ? i(t, o, r) : i(t, o)) || r);
        return a > 3 && r && Object.defineProperty(t, o, r), r;
    };
Object.defineProperty(i, "__esModule", {value: !0}), (i.bodyModel = void 0);
var s = function () {};
i.bodyModel = s;
var c = e("AssetsMap"),
    l = e("BundleNames"),
    d = e("GlobalParam"),
    u = e("Util"),
    p = e("ConfigGameData"),
    h = e("GameData"),
    f = e("GameOverData"),
    _ = e("nodePoolManager"),
    y = e("GameModuleViewMediator"),
    m = e("AIControl"),
    g = e("BaseControl"),
    v = e("SnakeUtil"),
    w = e("GameModuleView"),
    b = e("OtherControl"),
    C = e("PlayerControl"),
    k = e("snakeSkinControl"),
    N = cc._decorator,
    B = N.ccclass,
    P =
        (N.property,
        (function (e) {
            function t() {
                var t = (null !== e && e.apply(this, arguments)) || this;
                return (
                    (t.baseControl = null),
                    (t.mainplayer = !1),
                    (t.snakeName = ""),
                    (t.skin_index = 0),
                    (t.snake_index = 0),
                    (t.score = 0),
                    (t._life = 0),
                    (t.snakeUtil = null),
                    (t.snake_dance = 0),
                    (t.head_pos = null),
                    (t.tail_pos = null),
                    (t.snake_vector = null),
                    (t.target_vector = null),
                    (t.snake_angle = 0),
                    (t.isDie = !0),
                    (t.isIgnore = !1),
                    (t.snake_body = []),
                    (t.snake_body_length = 0),
                    (t.next_body_score = 30),
                    (t.snake_scale = 1),
                    (t.snakeIsShit = !1),
                    (t.enterGridIndex = -1),
                    (t.exitGridIndex = -1),
                    (t.snakeFalseLen = 0),
                    (t.now_speed = 1),
                    (t.snake_shit = !1),
                    (t._speed_maxScale = 1),
                    (t._speed_nowScale = 1),
                    (t._speed_minScale = 0.9),
                    (t._speed_scale_step = (1 - 0.9) / 20),
                    (t._snake_speed_dance = 1),
                    (t.snake_size = 60),
                    (t.eatSize = 60),
                    (t._snakeIsProp_Magnet = !1),
                    (t._prop_Magnet_Timer = 0),
                    (t._snakeIsProp_Double = !1),
                    (t._prop_Double_Timer = 0),
                    (t._isShield = !1),
                    (t._isShield_Timer = 0),
                    (t._MissilesNumber = 0),
                    (t.porp_shield_index = 0),
                    t
                );
            }
            return (
                a(t, e),
                Object.defineProperty(t.prototype, "life", {
                    get: function () {
                        return this._life;
                    },
                    set: function (e) {
                        e < 0 && (e = 0),
                            (this._life = e),
                            this.snakeType == h.SnakeType.Player &&
                                _.default.instance.gameModelInstance.refreshUILife(this._life);
                    },
                    enumerable: !1,
                    configurable: !0
                }),
                (t.prototype.init = function () {
                    (this.snakeUtil = new v.default(this.getCurrentScale())),
                        (this.node.name = this.snakeName),
                        (this.node.active = !0),
                        this.snakeSkinControl || (this.snakeSkinControl = this.node.addComponent(k.default)),
                        this.snakeSkinControl.init(this),
                        this.baseControl ||
                            (this.snakeType == h.SnakeType.Player
                                ? (this.baseControl = this.node.addComponent(C.default))
                                : this.snakeType == h.SnakeType.Other
                                ? (this.baseControl = this.node.addComponent(b.default))
                                : this.snakeType == h.SnakeType.AI &&
                                  (this.baseControl = this.node.addComponent(m.default))),
                        this.baseControl.init(),
                        (this.isDie = !1),
                        (this.life = d.default.GameConfigData.initStars),
                        (this.snake_dance = Number(p.default.SNAKE_SPEED_DANCE.toString())),
                        (this.tail_pos = this.head_pos),
                        (this.mainplayer = this.snakeName == h.default.instance.UserName),
                        (this._snake_speed_dance = Number(p.default.SNAKE_SPEED_DANCE.toString())),
                        (this.snake_body = []),
                        (this.snake_body_length = 0),
                        (this.snakeFalseLen = 0),
                        (this.now_speed = 1),
                        (this.next_body_score = 30),
                        (this.snake_size = 60),
                        (this.eatSize = Number(h.default.instance.eatSize.toString())),
                        (this.snake_scale = 1),
                        (this._snakeIsProp_Magnet = !1),
                        (this._snakeIsProp_Double = !1),
                        (this._isShield = !1),
                        (this._MissilesNumber = d.default.GameConfigData.MissilesNumber),
                        (this.snakeIsShit = !1),
                        this.unschedule(this.snakeTimingShit);
                    var e = u.default.randomFrom(-50, 50),
                        t = u.default.randomFrom(-50, 50);
                    this.snakeMoving(cc.v2(e, t)),
                        this.baseControl.refreshView_Missile(),
                        _.default.instance.gameModelInstance.initSnakeName(this.snakeName),
                        f.default.instance.set_nowScoreList(this),
                        this.initIgnore();
                }),
                (t.prototype.snakeMoving = function (e) {
                    this.isDie ||
                        ((null != this.snake_vector && this.snake_vector != cc.v2(0, 0)) ||
                            (this.snake_vector = e.normalize()),
                        (this.target_vector = e.normalize()));
                }),
                (t.prototype.updateSnakeVector = function () {
                    if (null == this.snake_vector || this.snake_vector == this.target_vector)
                        this.snake_vector = this.target_vector;
                    else {
                        var e = cc.v3(0, 0, 0),
                            t = cc.v3(150 * this.snake_vector.x, 150 * this.snake_vector.y, 0),
                            o = cc.v3(150 * this.target_vector.x, 150 * this.target_vector.y, 0),
                            n = p.default.GetAngleWith360(e, t, o);
                        n < 45 || n > 315
                            ? (this.snake_vector = this.target_vector)
                            : (n > 180 ? (this.snake_angle -= 20) : (this.snake_angle += 20),
                              this.snake_angle > 360 && (this.snake_angle -= 360),
                              this.snake_angle < -360 && (this.snake_angle += 360),
                              (this.snake_vector = p.default.angleToVector(this.snake_angle)));
                    }
                    this.snake_angle = p.default.vectorToAngle(this.snake_vector.normalize());
                }),
                (t.prototype.initIgnore = function () {
                    var e = this;
                    (this.isIgnore = !0),
                        this.snakeSkinControl.refreshShield(),
                        this.scheduleOnce(function () {
                            (e.isIgnore = !1), e.snakeSkinControl.refreshShield();
                        }, 5);
                }),
                (t.prototype.refreshSnakeBodySize = function () {
                    this.snake_body_length,
                        this.snake_shit
                            ? (this._speed_nowScale = this._speed_nowScale - this._speed_scale_step)
                            : (this._speed_nowScale = this._speed_nowScale + this._speed_scale_step),
                        this._speed_nowScale > this._speed_maxScale
                            ? (this._speed_nowScale = this._speed_maxScale)
                            : this._speed_nowScale < this._speed_minScale &&
                              (this._speed_nowScale = this._speed_minScale),
                        (this.snake_scale = this.getCurrentScale()),
                        (this._snake_speed_dance = p.default.SNAKE_SPEED_DANCE),
                        (this.snake_size = p.default.SNAKE_BODY_SIZE * this.snake_scale),
                        this.mainplayer
                            ? (this.eatSize = h.default.instance.eatSize * this.snake_scale)
                            : (this.eatSize = 57 * this.snake_scale);
                }),
                (t.prototype.getCurrentScale = function (e) {
                    return (
                        void 0 === e && (e = -1),
                        -1 == e && (e = this.snakeFalseLen),
                        e >= d.default.snakeMaxScaleNode
                            ? 2.9
                            : e > 275
                            ? 2.45 + 0.01 * (e - 275)
                            : e > 150
                            ? 1.45 + 0.008 * (e - 150)
                            : 1 + 0.003 * e
                    );
                }),
                (t.prototype.__update = function (e) {
                    if ((this.isStopPorp(e), h.default.instance.gameState == h.GameState.play && !this.isDie)) {
                        var t, o;
                        this.mainplayer && 1 != this.now_speed
                            ? ((this.snake_vector = this.target_vector),
                              (this.snake_angle = p.default.vectorToAngle(this.snake_vector.normalize())))
                            : this.updateSnakeVector();
                        for (var n, i = this.snake_body.length, a = 1; a <= this.now_speed; a++) {
                            (t = this.head_pos.x + this.snake_vector.x * this._snake_speed_dance * a),
                                (o = this.head_pos.y + this.snake_vector.y * this._snake_speed_dance * a);
                            for (var r = 0; r < i; r++)
                                0 == (m = i - r - 1)
                                    ? ((this.snake_body[m].angle = this.snake_angle),
                                      (this.snake_body[m].x = t),
                                      (this.snake_body[m].y = o))
                                    : ((this.snake_body[m].angle = this.snake_body[m - 1].angle),
                                      (this.snake_body[m].x = this.snake_body[m - 1].x),
                                      (this.snake_body[m].y = this.snake_body[m - 1].y));
                            var s = w.default.Insance.snakeGridMap,
                                c = s.getBlokInfoByPos(cc.v3(t, o));
                            this.enterIndex(c);
                            var l =
                                    this.snake_body.length > 0
                                        ? cc.v3(
                                              this.snake_body[this.snake_body.length - 1].x,
                                              this.snake_body[this.snake_body.length - 1].y
                                          )
                                        : cc.v3(t, o),
                                d = s.getBlokInfoByPos(cc.v3(l.x, l.y));
                            this.exitIndex(d);
                        }
                        if (((this.head_pos = cc.v3(t, o, 0)), this.addSnakeBody(), this.snake_body.length > 0)) {
                            var u = this.snake_body[this.snake_body.length - 1];
                            (this.tail_pos.x = u.x), (this.tail_pos.y = u.y);
                        }
                        this.refreshSnakeBodySize(), (n = this._snakeIsProp_Magnet ? 3 * this.eatSize : this.eatSize);
                        var y = _.default.instance.gameModelInstance._layer_food.isEatFood(this.head_pos, n);
                        if (y.length > 0)
                            for (var m = 0; m < y.length; m++) {
                                var g = y[m];
                                this.eatFood(g);
                            }
                        _.default.instance.gameModelInstance.refreshSnakeName(
                            this.snakeName,
                            this.head_pos,
                            this.snake_scale
                        ),
                            h.default.instance.setUserFraction(this.snakeName, this.score),
                            f.default.instance.set_nowScoreList(this),
                            this.baseControl.refreshCameraPos(this.head_pos, this.snake_scale),
                            this.snakeSkinControl.__update(e);
                    }
                }),
                (t.prototype.enterIndex = function (e) {
                    -1 != this.enterGridIndex &&
                        this.enterGridIndex != e.index &&
                        w.default.Insance.snakeGridMap.addBlockInfo(e.index, this.snakeName, this),
                        (this.enterGridIndex = e.index);
                }),
                (t.prototype.exitIndex = function (e) {
                    -1 != this.exitGridIndex &&
                        e.index != this.exitGridIndex &&
                        w.default.Insance.snakeGridMap.removeBlockInfo(this.exitGridIndex, this.snakeName),
                        (this.exitGridIndex = e.index);
                }),
                (t.prototype.setSnakeSpeed = function (e) {
                    if (!this.isDie)
                        if (e && this.score > 30) {
                            if (((this.now_speed = 2), this.snakeIsShit)) return;
                            (this.snakeIsShit = !0), this.schedule(this.snakeTimingShit, 0.25);
                        } else {
                            if (((this.now_speed = 1), !this.snakeIsShit)) return;
                            (this.snakeIsShit = !1), this.unschedule(this.snakeTimingShit);
                        }
                }),
                (t.prototype.addSnakeBody = function () {
                    var e = 10 * Math.floor((3 * this.snakeFalseLen) / 10);
                    if (
                        this.score > this.next_body_score + e &&
                        ((this.next_body_score += e),
                        this.snakeFalseLen++,
                        this.snake_body_length <= d.default.snakeMaxNode)
                    ) {
                        this.snake_body_length++;
                        for (var t = 0; t < this.snakeUtil.currentDistance; t++) this.addBody();
                        this.dynamicDistantFrame();
                    }
                }),
                (t.prototype.addBody = function () {
                    var e = new s();
                    (e.x = this.tail_pos.x), (e.y = this.tail_pos.y), (e.angle = 0), this.snake_body.push(e);
                }),
                (t.prototype.dynamicDistantFrame = function () {
                    var e = this.getCurrentScale(this.snake_body_length),
                        t = this.snakeUtil.addBodyDistance(e);
                    if (t > 0)
                        for (
                            var o = Math.min(d.default.snakeMaxNode, this.snake_body_length),
                                n = o * (this.snakeUtil.currentDistance - t),
                                i = o * this.snakeUtil.currentDistance - n,
                                a = 0;
                            a < i;
                            a++
                        )
                            this.addBody();
                }),
                (t.prototype.snakeTimingShit = function () {
                    if (!(this.isDie || this.snake_body.length <= 0)) {
                        if (this.score < 30) return this.setSnakeSpeed(!1);
                        this.score -= 1;
                        var e = this.snake_body[this.snake_body.length - 1];
                        _.default.instance.gameModelInstance._layer_food.addShitFood(cc.v2(e.x, e.y));
                    }
                }),
                (t.prototype.eatFood = function (e) {
                    if (
                        (this.baseControl.eatFood(e), this.snakeSkinControl.eatFood(e), e.foodType == h.FoodType.Magnet)
                    )
                        return this.eatPorp_Magnet();
                    if (e.foodType == h.FoodType.Double) return this.eatPorp_Double();
                    if (e.foodType == h.FoodType.Missile) return this.eatPorp_Missile();
                    if (e.foodType == h.FoodType.Shield) return this.eatPorp_Shield();
                    if (e.foodType == h.FoodType.Stars) return this.eatPorp_Stars();
                    var t = 1;
                    this.mainplayer && h.default.instance.profit && (t = h.default.instance.profit + 1),
                        this._snakeIsProp_Double ? (this.score += 2 * e.score * t) : (this.score += e.score * t);
                }),
                (t.prototype.snakeDieAddFood = function () {
                    for (var e = this.snake_body, t = [], o = 0; o < e.length; o++) o % 6 == 0 && t.push(e[o]);
                    _.default.instance.gameModelInstance._layer_food.SnakeDieAddFood(t, this.snake_scale);
                }),
                (t.prototype.onSnakeDie = function () {
                    if (!this.isDie) {
                        w.default.Insance.snakeGridMap.clearBlockPartElement(this.snakeName),
                            (this.isDie = !0),
                            this.snakeSkinControl.onSnakeDie(),
                            (this.node.active = !1);
                        var e = game.BundleManager.instance
                            .getBundle(l.default.Game)
                            .get(c.default.GameBundle.sounds.assetsList.die, cc.AudioClip);
                        game.AudioManager.playEffect(e),
                            _.default.instance.gameModelInstance.isShowSnakeName(this.snakeName),
                            (this.isDie = !0),
                            this.mainplayer || h.default.instance.setUserFraction(this.snakeName, 0, !0),
                            h.default.instance.GameModel == h.GameModel.eatChicken
                                ? (this.mainplayer || this.snakeDieAddFood(),
                                  this.mainplayer &&
                                      h.default.instance.tt_recorder_flags &&
                                      _.default.instance.gameModelInstance.onClick_btn_recorder())
                                : h.default.instance.GameModel == h.GameModel.TimeLimit
                                ? (f.default.instance.set_oldScoreList(this),
                                  this.snakeDieAddFood(),
                                  this.mainplayer
                                      ? w.default.Insance.playAgain(3)
                                      : w.default.Insance._layer_snake.newSnake(this.snake_index, 3))
                                : h.default.instance.GameModel == h.GameModel.endless &&
                                  (this.mainplayer
                                      ? (h.default.instance.tt_recorder_flags &&
                                            _.default.instance.gameModelInstance.onClick_btn_recorder(),
                                        h.default.instance.againPlayTimes >= 3 &&
                                        ad.curr_platform != window.ad.Platform.ANDROID
                                            ? game.PopupManager.instance.showPopView(
                                                  c.default.PopupMap.ProgressTaskView,
                                                  null,
                                                  !1,
                                                  !1
                                              )
                                            : (w.default.Insance.hideOppoBanner(),
                                              w.default.Insance.hideVivoBanner(),
                                              h.default.instance.isStoreHose &&
                                                  game.appFacade.sendNotification(y.default.STOP_STORE_TIME),
                                              ad.curr_platform == window.ad.Platform.OPPO &&
                                              ad.adInterface.isNativePatchReady()
                                                  ? game.PopupManager.instance.showPopView(
                                                        c.default.PopupMap.CountDownView2,
                                                        {snakeName: this.snakeName},
                                                        !1,
                                                        !1
                                                    )
                                                  : game.PopupManager.instance.showPopView(
                                                        c.default.PopupMap.CountDownView,
                                                        {snakeName: this.snakeName},
                                                        !1,
                                                        !1
                                                    )))
                                      : (this.snakeDieAddFood(),
                                        w.default.Insance._layer_snake.newSnake(this.snake_index)));
                    }
                }),
                (t.prototype.isStopPorp = function (e) {
                    this._snakeIsProp_Magnet &&
                        ((this._prop_Magnet_Timer += e),
                        this._prop_Magnet_Timer > 10 &&
                            ((this._snakeIsProp_Magnet = !1), (this._prop_Magnet_Timer = 0)),
                        this.baseControl.refreshView_Magnet()),
                        this._snakeIsProp_Double &&
                            ((this._prop_Double_Timer += e),
                            this._prop_Double_Timer > 10 &&
                                ((this._snakeIsProp_Double = !1), (this._prop_Double_Timer = 0)),
                            this.baseControl.refreshView_Double()),
                        this._isShield &&
                            ((this._isShield_Timer += e),
                            this._isShield_Timer > 10 &&
                                ((this._isShield = !1),
                                (this._prop_Double_Timer = 0),
                                this.snakeSkinControl.refreshShield()));
                }),
                (t.prototype.eatPorp_Magnet = function () {
                    (this._snakeIsProp_Magnet = !0), (this._prop_Magnet_Timer = 0);
                }),
                (t.prototype.eatPorp_Double = function () {
                    (this._snakeIsProp_Double = !0), (this._prop_Double_Timer = 0);
                }),
                (t.prototype.eatPorp_Missile = function () {}),
                (t.prototype.eatPorp_Shield = function () {
                    this.porp_shield_index++, this.baseControl.refreshView_Shield();
                }),
                (t.prototype.eatPorp_Stars = function (e) {
                    void 0 === e && (e = !0),
                        e
                            ? (this.life += d.default.GameConfigData.addStars)
                            : (this.life -= d.default.GameConfigData.reduceStars),
                        this.snakeSkinControl.show_blood(e);
                }),
                (t.prototype.use_Shield = function () {
                    if (this.porp_shield_index < 10) return console.log("保护罩不足！：" + this.porp_shield_index);
                    (this.porp_shield_index -= 10),
                        this.baseControl.refreshView_Shield(),
                        (this._isShield = !0),
                        (this._isShield_Timer = 0),
                        this.snakeSkinControl.refreshShield();
                }),
                (t.prototype.use_Missile = function () {
                    if (0 != this._MissilesNumber) {
                        var e = this;
                        if (this._MissilesNumber == d.default.GameConfigData.MissilesNumber) {
                            var t = function () {
                                cc.tween({a: 0})
                                    .to(
                                        2,
                                        {a: 100},
                                        {
                                            progress: function (t, o, n, i) {
                                                var a = t + (o - t) * i;
                                                return (
                                                    e.mainplayer &&
                                                        _.default.instance.gameModelInstance &&
                                                        _.default.instance.gameModelInstance.refreshPorp_MissileProgress(
                                                            a
                                                        ),
                                                    a
                                                );
                                            }
                                        }
                                    )
                                    .call(function () {
                                        e._MissilesNumber++,
                                            e.baseControl && e.baseControl.refreshView_Missile(),
                                            e._MissilesNumber != d.default.GameConfigData.MissilesNumber && t();
                                    })
                                    .start();
                            }.bind(this);
                            t();
                        }
                        this._MissilesNumber--,
                            this.baseControl.refreshView_Missile(),
                            _.default.instance.gameModelInstance.sendMissile(
                                this.snakeName,
                                this.head_pos,
                                this.snake_vector,
                                this.skin_index,
                                this.snake_angle
                            );
                    }
                }),
                (t.prototype.showMissileSize = function (e) {
                    this.snakeSkinControl.showMissileSize(e);
                }),
                (t.prototype.playAgain = function () {
                    _.default.instance.gameModelInstance.initSnakeName(this.snakeName),
                        (this.life = d.default.GameConfigData.initStars),
                        this.resetSnakePos(),
                        (this.node.active = !0),
                        (this.isDie = !1),
                        this.setSnakeSpeed(!1),
                        this.initIgnore(),
                        (this._prop_Magnet_Timer = 10),
                        (this._prop_Double_Timer = 10),
                        f.default.instance.set_nowScoreList(this);
                }),
                (t.prototype.resetSnakePos = function (e) {
                    void 0 === e && (e = null);
                    var t = p.default.GetResurrectionPos(),
                        o = t[u.default.randomFrom(0, t.length - 1)];
                    if (
                        (e && (o = e),
                        (this.head_pos = o),
                        (this.tail_pos = o),
                        h.default.instance.GameModel == h.GameModel.TimeLimit)
                    )
                        (this.score = 100),
                            (this.next_body_score = 30),
                            (this.snake_body = []),
                            (this.snake_body_length = 0),
                            (this.snakeFalseLen = 0),
                            this.snakeSkinControl.init(this);
                    else
                        for (var n = 0, i = this.snake_body.length; n < i; n++)
                            (this.snake_body[n].angle = 0), (this.snake_body[n].x = o.x), (this.snake_body[n].y = o.y);
                }),
                r([B], t)
            );
        })(g.default));
i.default = P;
